package com.hunttown.mes.common;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * created by hunttown on 2017/7/27
 */
public class OperateCode {
    private static final int SUCCESS_CODE = 1;
    private static final String SUCCESS_MESSAGE = "操作成功！";
    private static final int FAIL_CODE = 0;
    private static final String FAIL_MESSAGE = "操作失败，请检查提交内容！";
    private static final int ERROR_CODE = -1;
    private static final String ERROR_MESSAGE = "服务器离家出走了，请联系开发工程师！";
    private static final String CHARSET_UTF8 = "UTF-8";
    private static final String MESSAGE_BYSELF = "自定义";
    private static final String DENIED_MESSAGE = "对不起，你没有操作权限！";

    //java编码：
    //URLEncoder.encode(SUCCESS_MESSAGE,"UTF-8");
    //java解码：
    //URLDecoder.decode(SUCCESS_MESSAGE,"UTF-8");

    //javascript编码：
    //encodeURI(SUCCESS_MESSAGE);    将SUCCESS_MESSAGE转换成utf-8。
    //javascript解码：
    //decodeURI(SUCCESS_MESSAGE);    将utf-8的SUCCESS_MESSAGE反转成字符串。

    /**
     * 结果组装
     *
     * @param map  传入/返回结果
     * @param type 类型：1成功 0失败 -1程序出错
     * @return
     */
    public static Map<String, String> CompositionResult(Map<String, String> map, Integer type) {
        return CompositionResult(map, type, MESSAGE_BYSELF);
    }

    /**
     * 结果组装
     *
     * @param map     传入/返回结果
     * @param type    类型：1成功 0失败 -1程序出错
     * @param message 自定义出错信息
     * @return
     */
    public static Map<String, String> CompositionResult(Map<String, String> map, Integer type, String message) {
        if (map == null) {
            map = new HashMap<String, String>();
        }

        try {
            switch (type) {
                case 1: {
                    map.put("returnCode", String.valueOf(SUCCESS_CODE));
                    map.put("returnMessage", URLEncoder.encode(SUCCESS_MESSAGE, CHARSET_UTF8));
                }
                ;
                break;
                case 0: {
                    map.put("returnCode", String.valueOf(FAIL_CODE));
                    map.put("returnMessage", URLEncoder.encode(FAIL_MESSAGE, CHARSET_UTF8));
                }
                ;
                break;
                case -1: {
                    map.put("returnCode", String.valueOf(ERROR_CODE));
                    map.put("returnMessage", URLEncoder.encode(ERROR_MESSAGE, CHARSET_UTF8));
                }
                ;
                break;
                case -10:
                case 10: {  //-10 和 10 为自定义信息
                    map.put("returnCode", String.valueOf(type));
                    map.put("returnMessage", URLEncoder.encode(message, CHARSET_UTF8));
                }
                break;
                case -15: {
                    map.put("returnCode", String.valueOf(type));
                    map.put("returnMessage", URLEncoder.encode(DENIED_MESSAGE, CHARSET_UTF8));
                }
                ;
                break;
                default: {
                    map.put("returnCode", String.valueOf(type));
                    map.put("returnMessage", URLEncoder.encode(message, CHARSET_UTF8));
                }
                break;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return map;
    }
}
